package com.cb.service.impl;

import com.alibaba.fastjson.JSON;
import com.cb.dao.ConductorVipDao;
import com.cb.pojo.VipList;
import com.cb.service.ConductorVipService;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ConductorVipServiceImpl implements ConductorVipService {

    @Autowired
    private ConductorVipDao conductorVipDao;

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Override
    public String getAllVipList(HashMap map) {
        String yyid = String.valueOf(map.get("yyid"));
        int pageNum = Integer.parseInt(String.valueOf(map.get("page")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("size")));
        PageHelper.startPage(pageNum, pageSize);
        List<VipList> vips = conductorVipDao.getAllVipList(yyid);
        String filmsJson = JSON.toJSONString(vips);
        return filmsJson;
    }

    @Override
    public String getVip(HashMap map) {
        List<VipList> vips;
        int pageNum = Integer.parseInt(String.valueOf(map.get("page")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("size")));
        //1:未到期，0：到期
        if (map.get("code").equals("1")){
            PageHelper.startPage(pageNum, pageSize);
            vips = conductorVipDao.getVipIng(String.valueOf(map.get("yyid")));
        }
        else{
            PageHelper.startPage(pageNum, pageSize);
            vips = conductorVipDao.getVipEnd(String.valueOf(map.get("yyid")));
        }
        String vipsJson = JSON.toJSONString(vips);
        return vipsJson;
    }

    @Override
    public String getVipIng(String yyid) {
        List<VipList> vips = conductorVipDao.getVipIng(yyid);
        String vipsJson = JSON.toJSONString(vips);
        return vipsJson;
    }

    @Override
    public String getVipEd(String yyid) {
        List<VipList> vips = conductorVipDao.getVipEnd(yyid);
        String vipsJson = JSON.toJSONString(vips);
        return vipsJson;
    }

    @Override
    public String searchVip(HashMap map) {
        VipList vip = conductorVipDao.searchVip(map);
        String vipJson = JSON.toJSONString(vip);
        return vipJson;
    }

    @Override
    public String createVipOrder(HashMap map){
        return "s";
    }

    @Override
    public int renewVip(HashMap map) {
        logger.info("——————————  "+map.get("username")+" "+map.get("yyid")+" "+map.get("i"));
        if(conductorVipDao.renewVip(map)==1){
            return 1;
        }
        else{
            return -1;
        }
    }

    @Override
    public int updateVip(HashMap map) {
        logger.info("——————————  "+map.get("username")+" "+map.get("yyid")+" "+map.get("i"));
        if(conductorVipDao.updateVip(map)==1){
            return 1;
        }
        else{
            return -1;
        }
    }

    /**
     * 售票员为用户办理会员（未到期就自动续费，到期就重新开通,没有办理过就新增）（username,yyid,i）
     * alipay参数：订单号，（产品名）电影名，应付金额，类型，倒计时2min，产品id（随便写）
     * */
    @Override
    public int vipInsert(HashMap map) throws ParseException {

        String yyid = String.valueOf(map.get("yyid"));
        String username = String.valueOf(map.get("username"));
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        VipList vip = conductorVipDao.searchVip(map);
        Date getEndDate = formatter.parse(String.valueOf(vip.getEndtime()));
        if (vip != null){
            if (getEndDate.compareTo(now) >= 0){
                if (conductorVipDao.renewVip(map) == 1)
                    return 2; //续费成功
                else
                    return 3; //续费失败
            }
            else{
                if (conductorVipDao.updateVip(map) == 1)
                    return 4; //重新开通
                else
                    return 5; //重新开通失败
            }

        }
        else {
            if (conductorVipDao.vipInsert(map) == 1)
                return 1; //新增成功
            else
                return 0; //新增失败
        }
    }
}
